//
//  WelcomeViewController.swift
//  WeiBo
//
//  Created by 陈贵 on 15/9/10.
//  Copyright © 2015年 hzj. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    //记录头像底边当前约束
   private var currentConstraints:NSLayoutConstraint?
    

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
/// 记录头像底部的约束
        let bh = -(UIScreen.mainScreen().bounds.height + currentConstraints!.constant)
        
        currentConstraints?.constant = bh
        
        textLable.alpha = 0
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            //重新布局
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.75, animations: { () -> Void in
                    self.textLable.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(HZJselectTheControllerNotification, object: "main")
                })
        }


        
    }
    
    
    ///加载控件
    private func setUI(){
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(textLable)
        
        
        ///约束
        backImageView.frame = view.bounds
        
//        backImageView.translatesAutoresizingMaskIntoConstraints = false
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v": backImageView]))
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-0-|", options: [], metrics: nil, views: ["v": backImageView]))
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy:
            NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -200))
        
       self.currentConstraints = view.constraints.last
        
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
        
        textLable.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: textLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint( NSLayoutConstraint(item: textLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 25))
    }
    
    
   
    
/// MARK: - 懒加载控件
    ///背景视图
    private lazy var backImageView:UIImageView = {
        let bv = UIImageView()
        bv.image = UIImage(named: "ad_background")
        return bv
    }()
/// 头像
    private lazy var iconView:UIImageView = {
        let icon = UIImageView()
        icon.sd_setImageWithURL(UserAccountViewModel.sharedUserAccountViewModel.avatarlarge)
        icon.clipsToBounds = true
        icon.layer.cornerRadius = 45
        return icon
    }()
    
    ///欢迎文字
    private lazy var textLable:UILabel = UILabel(title: "蓬荜生辉", color: UIColor(white: 0.7, alpha: 1), fontSize: 17,layoutWidth:0)
    
}
